package com.itboye.ihomebank.activity.login;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.itboye.ihomebank.R;
import com.itboye.ihomebank.base.BaseOtherActivity;
import com.itboye.ihomebank.bean.ResultEntity;
import com.itboye.ihomebank.constants.SPContants;
import com.itboye.ihomebank.presenter.UserPresenter;
import com.itboye.ihomebank.util.ByAlert;
import com.itboye.ihomebank.util.SPUtils;

import java.util.Observable;

/**
 * Created by Administrator on 2018/3/26 0026.
 */

public class ForgetPasswordActivity extends BaseOtherActivity {
    //title
    private View v_statusbar;
    private ImageView close_icon;
    private TextView add_lift_tv,add_shap_title_tv;

    private EditText etPhone,verifyCode,resetPassword;
    private TextView sendCode,confirmPass;

    private UserPresenter userPresenter;
    @Override
    protected int layout_id() {
        return R.layout.activity_forgetactivity;
    }

    @Override
    protected void onInitController() {

    }

    @Override
    protected void onInit() {
     setStatusBarHeight(v_statusbar);
     add_shap_title_tv.setText("忘记密码");
     userPresenter = new UserPresenter(this);

    }

    @Override
    protected void onReadIntent(Bundle bundle) {

    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()){
            case R.id.close_icon:
                this.finish();
                break;
            case R.id.sendCode://发送验证码
                if ("".equals(etPhone.getText().toString())){
                    ByAlert.alert("手机号不能为空");
                }else {
                    getyzm();
                }
                break;

            case R.id.confirmPass: //确定
                VerificationYZM();
                break;
        }
    }

    /**
     * 获取验证码
     */
    private void getyzm() {
        showProgressDialog("请求中,请稍后...",true);
        userPresenter.send(etPhone.getText().toString(), "2");
    }

    /**
     * 验证验证码是否正确
     */
    private void VerificationYZM() {
        if (verifyCode.getText().toString().trim().equals("")) {
            ByAlert.alert("请输入验证码");
            return;
        }
        userPresenter.verification(etPhone.getText().toString(), "2", verifyCode.getText().toString());
    }

    /**
     * 修改密码
     */
    private void changepassword() {
        if (resetPassword .equals("")) {
            ByAlert.alert("请输入新的密码");
            return;
        }
        userPresenter.backPass(etPhone.getText().toString(), verifyCode.getText().toString(), resetPassword.getText().toString());
    }

    @Override
    public void update(Observable observable, Object data) {
        ResultEntity entity=handlerError(data);
        try {
            closeProgressDialog();
        } catch (Exception e) {
        }
        if(entity!=null){
            if(!entity.getCode().equals(0)){
                if(entity.getEventType()==UserPresenter.send_success){
                    String bean=(String) entity.getData();
                    if(bean!=null){
                        ByAlert.alert("验证码发送成功");
                    }else{
                        ByAlert.alert("失败");
                    }
                }else if(entity.getEventType() == UserPresenter.send_fail){
                    ByAlert.alert("验证码发送失败");
                }else if (entity.getEventType() == UserPresenter.verification_success) {
                    changepassword();
                }else if (entity.getEventType() == UserPresenter.verification_fail) {
                    ByAlert.alert(entity.getData());
                }else if(entity.getEventType()==UserPresenter.backPass_success){
                    String bean=(String) entity.getData();
                    Log.i("result", bean.toString());
                    if(bean!=null){
                        SPUtils.put(this,null, SPContants.PASSWORD,resetPassword.getText().toString());
                        ByAlert.alert("修改成功");
                        finish();
                    }
                }else if(entity.getEventType() == UserPresenter.backPass_fail){
                    ByAlert.alert("修改失败");

                }
            }
        }

    }

}
